package com.cskaoyan.controller.back;

import com.cskaoyan.bean.po.AdminInfoBean;
import com.cskaoyan.bean.po.InfoData;
import com.cskaoyan.bean.po.LoginUserData;
import com.cskaoyan.bean.po.MarketAdmin;
import com.cskaoyan.bean.vo.back.BaseRespVo;
import com.cskaoyan.bean.vo.back.DashBoardCountVO;
import com.cskaoyan.config.realm.MarketToken;
import com.cskaoyan.service.back.AuthService;
import com.cskaoyan.service.back.RoleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 身份验证、首页显示XXX数量控制器
 *
 * @author 贾志超
 * @date 2022/10/11
 *///shiro整合之后，在做具体的开发
//响应结果都是JSON，Controller组件上我们都用@RestController注解
@RestController
@RequestMapping("admin")
public class AuthController {

    @Autowired
    AuthService authService;
    @Autowired
    RoleService roleService;

    /**
     * 管理员登录
     * jzc
     * http://localhost:8083/admin/auth/login
     * @param map
     * @return {@link BaseRespVo}<{@link LoginUserData}>
     */

    @PostMapping("auth/login")
    public BaseRespVo<LoginUserData> login(@RequestBody Map map) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");

        // TODO: 已整合Shiro安全框架的内容
        // 获取操作主体
        Subject subject = SecurityUtils.getSubject();
//        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);

        MarketToken usernamePasswordToken = new MarketToken(username, password, "admin");


        // 认证，最终会执行到realm.doGetAuthenticationInfo
        try {
            subject.login(usernamePasswordToken);
        }catch (Exception e){
            e.printStackTrace();
            return BaseRespVo.unAuthc1();
        }
        // 是否认证成功
        boolean authenticated = subject.isAuthenticated();
        if (!authenticated){
            // 认证失败
            System.out.println("认证失败");
        }
        // 获得session信息
        Session session = subject.getSession();
        MarketAdmin principal = (MarketAdmin) subject.getPrincipal();

        LoginUserData loginUserData = new LoginUserData();
        AdminInfoBean adminInfo = new AdminInfoBean();
        adminInfo.setAvatar(principal.getAvatar());
        adminInfo.setNickName(principal.getUsername());
        loginUserData.setAdminInfo(adminInfo);
        loginUserData.setToken(((String) session.getId()));

        return BaseRespVo.ok(loginUserData);
    }

    /**
     * 管理员注销
     * jzc
     * http://localhost:8083/admin/auth/logout
     * @return {@link BaseRespVo}
     */
    @PostMapping("auth/logout")
    public BaseRespVo logout() {
        //TODO:已整合shiro框架内容
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.ok();
    }

    /**
     * 显示主页的商品、用户、订单、货品数量
     * jzc
     * http://localhost:8083/admin/dashboard
     * @return {@link BaseRespVo}
     */
    @GetMapping("dashboard")
    public BaseRespVo dashboard() {
        DashBoardCountVO dashBoardCountVO = authService.getAllGoodsAndUserAndProductAndOrderCount();
        return BaseRespVo.ok(dashBoardCountVO);
    }

    /**
     * 显示信息
     * jzc
     * http://localhost:8083/admin/auth/info?token=c4d988f3-e4c5-4e46-b6ce-13b9e008ea20
     * @param token
     * @return {@link BaseRespVo}
     */
    @RequestMapping("auth/info")
    public BaseRespVo info(String token) {
        //TODO:已整合shiro框架内容
        InfoData infoData=roleService.queryRoleInfo();
        return BaseRespVo.ok(infoData);
    }
    @GetMapping("noAuthc")
    public BaseRespVo noAuthc() {
        return BaseRespVo.ok("没有权限");
    }
}
